package org.yoki.edu.portal.core.radius.domain;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunzhitx.cloud.common.model.PageResult;
import com.yunzhitx.cloud.common.model.ddd.AbstractEntity;
import com.yunzhitx.cloud.common.model.ddd.InstanceFactory;
import com.yunzhitx.cloud.common.model.exception.BizException;
import lombok.Data;
import org.yoki.edu.portal.core.exception.BasicRestStatus;
import org.yoki.edu.portal.core.radius.infra.NasRepository;

import javax.persistence.*;
import java.io.Serializable;

/**
 * 代码生成器自动生成
 * Date:2017-10-29 17:24:25
 *
 * @author
 */
@Data
@Table(name = "nas")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Nas implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id; //
    @Column(name = "nasname")
    private String nasname; //
    @Column(name = "shortname")
    private String shortname; //
    @Column(name = "type")
    private String type; //
    @Column(name = "ports")
    private Integer ports; //
    @Column(name = "secret")
    private String secret; //
    @Column(name = "server")
    private String server; //
    @Column(name = "community")
    private String community; //
    @Column(name = "description")
    private String description; //

    public Nas() {
    }

    public Nas(Integer id) {
        setId(id);
    }

    public Serializable getKey() {
        return getId();
    }

    public boolean existed() {
        return false;
    }

    public void save() throws Exception {
        if (existed()) {
            throw new BizException(BasicRestStatus.repeatDataErrorStatus);
        }
        getNasRepository().insert(this);
    }

    public static void delete(Integer[] ids) throws Exception {
        for (Integer id : ids) {
            getNasRepository().deleteByPrimaryKey(id);
        }
    }

    public void update() throws Exception {
        getNasRepository().updateByPrimaryKeySelective(this);
    }

    public Nas detail() {
        return getNasRepository().selectByPrimaryKey(getKey());
    }

    public static PageResult<Nas> page(PageResult<Nas> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        PageInfo<Nas> pageInfo = new PageInfo<>(getNasRepository().selectAll());
        page.setTotal(pageInfo.getTotal());
        page.setPageData(pageInfo.getList());

        return page;
    }

    private static NasRepository nasRepository;

    public static NasRepository getNasRepository() {
        if (null == nasRepository) {
            nasRepository = InstanceFactory.getInstance(NasRepository.class);
        }
        return nasRepository;
    }


}